// pages/admin/admin.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: '0',
    mycurrent: '审核通过',
    page: 1,
    list: [],
    rowsTotal: '',
    imgApi: app.globalData.imgApi,
    uList: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  MyonChange(e) {
    console.log('onChange', e)
    this.setData({
      mycurrent: e.detail.key,
    })
    switch (e.detail.key) {
      case '审核通过':
        this.setData({
          list: []
        })
        this.data.page = 1
        this.getList(1)
        break
      case '未审核':
        this.setData({
          list: []
        })
        this.data.page = 1
        this.getList(1)
        break
      // case '账号管理':
      //   this.userList()
      //   break

    }
  },
  search: function(e) {
    var that = this
    wx.request({
      url: app.globalData.api + 'designDiagram/list',
      data: {
        page: 1,
        rows: 99,
        status: this.data.mycurrent,
        keyword: e.detail.value.key
      },
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function(res) {
        if (res.data.code == 200) {
          that.data.list = []
          var myListImg = []
          for (var i in res.data.data.designDiagramList) {
            that.data.list.push(res.data.data.designDiagramList[i])
            myListImg.push(res.data.data.designDiagramList[i].designImage.split(',')[0])
          }
          that.setData({
            list: that.data.list,
            myListImg: myListImg,
          })
        } else {
          that.setData({
            list: []
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this
    // this.userList()
    this.setData({
      list: []
    })
    this.data.page = 1
    this.getList(1)
  },

  getList: function(page) {
    var that = this
    wx.request({
      url: app.globalData.api + 'designDiagram/list',
      data: {
        page: page,
        rows: 20,
        status: this.data.mycurrent
      },
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          var myListImg = []
          for (var i in res.data.data.designDiagramList) {
            that.data.list.push(res.data.data.designDiagramList[i])
            myListImg.push(res.data.data.designDiagramList[i].designImage.split(',')[0])
          }
          that.setData({
            list: that.data.list,
            myListImg: myListImg,
            rowsTotal: res.data.data.rowsTotal
          })
          that.data.page += 1
        }else{
          if (that.data.rowsTotal == ''){
            that.setData({
              list: []
            })
          }
        }
      }
    })
  },
  onReachBottom: function() {
    console.log('e');
    if (this.data.rowsTotal > this.data.list.length) {
      this.getList(this.data.page)
    }
  },
  goSDet: function(e) {
    wx.navigateTo({
      url: '/pages/S-details/S-details?id=' + e.currentTarget.dataset.id
    })
  },
  goDet: function(e) {
    wx.navigateTo({
      url: '/pages/S-details/S-details?id=' + e.currentTarget.dataset.id
    })
  },
  addAccount: function() {
    wx.navigateTo({
      url: '/pages/account/account?type=设计师'
    })
  },
  // userList: function() {
  //   var that = this
  //   wx.request({
  //     url: app.globalData.api + 'user/list',
  //     data: {
  //       page: 1,
  //       rows: 999,
  //       roleName: '设计师'
  //     },
  //     method: 'POST',
  //     header: {
  //       Authorization: wx.getStorageSync('token')
  //     },
  //     success: function(res) {
  //       console.log(res)
  //       if (res.data.code == 200) {
  //         that.setData({
  //           uList: res.data.data.userList
  //         })
  //       }
  //     }
  //   })
  // },
  deleUser: function(e) {
    var that = this
    wx.showModal({
      title: '提示',
      content: '确认删除?',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.api + 'user/' + e.currentTarget.dataset.id,
            data: {},
            header: {
              Authorization: wx.getStorageSync('token')
            },
            method: 'DELETE',
            success: (res) => {
              if (res.data.code == 200) {
                wx.showToast({
                  title: '删除成功',
                })
                // that.userList()
              }
            }
          })
        }
      }
    })
  },
  edlitUser: function(e) {
    wx.navigateTo({
      url: '/pages/account/account?type=设计师&id=' + e.currentTarget.dataset.id,
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})